import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'index.dart';

/// 滚动视图
class CarouselWidget extends StatelessWidget {
  const CarouselWidget({
    Key? key,
    this.onPageChanged,
    this.onTap,
    required this.items,
    this.currentIndex,
    this.height,
    this.indicatorColor,
    this.indicatorCircle,
    this.indicatorAlignment,
    this.indicatorLeft,
    this.indicatorRight,
    this.indicatorBottom,
    this.boxFit,
    this.scrollDirection = Axis.horizontal,
    this.showIndicator = false,
    this.autoPlay = true,
    this.enableInfiniteScroll,
    this.autoPlayInterval = const Duration(seconds: 4),
  }) : super(key: key);

  /// 切换页码
  final Function(int, CarouselPageChangedReason)? onPageChanged;

  /// 点击
  final Function(int index)? onTap;

  /// 数据列表
  final List<Widget> items;

  /// 当前选中
  final int? currentIndex;

  /// 高度
  final double? height;

  /// 指示器 颜色
  final Color? indicatorColor;

  /// 指示器 是否圆形
  final bool? indicatorCircle;

  /// 指示器 对齐方式
  final MainAxisAlignment? indicatorAlignment;

  /// 指示器 位置
  final double? indicatorLeft, indicatorRight, indicatorBottom;

  /// 图片裁剪模式
  final BoxFit? boxFit;

  /// 滚动方向
  final Axis scrollDirection;

  ///是否需要指示器
  final bool showIndicator;

  ///自动播放
  final bool autoPlay;

  ///循环
  final bool? enableInfiniteScroll;

  final Duration autoPlayInterval;

  Widget _buildView() {
    List<Widget> ws = [
      // 滚动视图
      CarouselSlider(
        options: CarouselOptions(
          // 高度
          height: height,
          // 填充比例
          viewportFraction: 1,
          // 图像比例
          aspectRatio: 3.2 / 1,
          // 确定当前页面是否应该大于侧面图像， 在旋转木马中营造一种深度感。
          enlargeCenterPage: false,
          // 循环
          enableInfiniteScroll: enableInfiniteScroll ?? items.length > 1,
          // 自动播放
          autoPlay: autoPlay,
          // 回调页切换事件
          onPageChanged: onPageChanged,
          // 滚动方向
          scrollDirection: scrollDirection,
          // 切换间隔
          autoPlayInterval: autoPlayInterval,
        ),
        items: <Widget>[
          for (var i = 0; i < items.length; i++)
            FractionallySizedBox(
              widthFactor: 1,
              child: ButtonWidget(
                hasInkWell: false,
                onTap: () {
                  if (onTap != null) onTap!(i);
                },
                child: items[i],
              ),
            ),
        ],
      ),

      // 指示器
      if (items.length > 1 && showIndicator)
        Positioned(
          left: indicatorLeft ?? 20.w,
          right: indicatorRight ?? 20.w,
          bottom: indicatorBottom ?? 10.w,
          child: SliderIndicatorWidget(
            // 个数
            length: items.length,
            // 当前索引
            currentIndex: currentIndex ?? 0,
            // 颜色
            color: indicatorColor ?? Colors.white,
            // 是否圆形
            isCircle: indicatorCircle ?? false,
            // 对齐方式
            alignment: indicatorAlignment ?? MainAxisAlignment.center,
          ),
        ),
    ];

    return Stack(children: ws);
  }

  @override
  Widget build(BuildContext context) {
    return _buildView();
  }
}
